var app = getApp();
let constants = require('../../common/Constants.js');
let network_utils = require('../../http/NetworkUtils.js');
import DeployingList from '../../templates/DeployingList/DeployingList';

var imPayArr = [{ code: '0', title: '单个(小程序录入)' }, { code: '1', title: '批量(PC后台录入)' }, { code: '2', title: '全部'}]

var posArr = constants.StoreStates.showPos
posArr.push({ code: "all", title:"全部"})
Page(Object.assign({
  pageNo: "1",
  data: {
    currtabState: false,//为true 尖尖朝下
    isHiddenMenu: true,
    storeList: [],
    hasMore: false,
    pickerName: '部署方式',
    array: imPayArr,
    index: 0, //立马付入网方式  默认0为单个
    swipertab: [{ name: '立马付', tabIndex: 1 }, { name: 'POS机', tabIndex: 2 }],
    currtabIndex: 1 , //当前tab号1立马付 2POS机
    posState:"all"  //POS审核状态 TODO  改为代码
  },
  
  onLoad: function (options) {
    app.checkLoginState(options, {
      onSkipLogin: () => {
      },

      onLogin: () => {
        wx.navigateTo({
          url: '../login/login?from=' + constants.LoginFrom.LOGIN
        });
      },

      onAutoLogin: (loginToken) => {
        wx.navigateTo({
          url: '../login/login?loginToken=' + loginToken + '&from=' + constants.LoginFrom.AUTO_LOGIN
        });
      }
    });
    if(options.pruductId==1){
      this.setData({
        currtabIndex:2
      })
    }
    // options.fromPush = true
    if (options.fromPush && options.store) {
      wx.navigateTo({
        url: './deploymentDetails/deploymentDetails?storeId=' + options.store + '&fromType=agentList'
      });
      // return
    } else if (options.fromPush && options.currtabIndex){
      this.setData({
        currtabIndex: options.currtabIndex
      })
    }


    this.pageNo = "1";
  },

  onShow: function () {
    if (!app.globalData.states.isLoginProcess) {
      this.onRefresh();
    }
  },

  tapLayor: function () {
    this.setData({
      isHiddenMenu: !this.data.isHiddenMenu,
    });
  },
  catchtap: function () { },
  /**
      * @Explain：选项卡点击切换 接入POS
      */
  tabSwitch: function (e) {
    //TODO  一个表示立马付或者POS的参数传给后台做网络请求
    if (e.currentTarget.dataset.current == 1) {
      //立码付
      this.setData({
        index: 0,
        pickerName: '部署方式',
        array: imPayArr,
        posState:""  //TODO  POS全部的代码
      })
    } else {
      this.setData({
        pickerName: '审核状态',
        array: posArr,  
        index:0   //立马付录入方式  个人    
      })
    }
    if (this.data.currtabIndex != e.currentTarget.dataset.current) {
      this.setData({
        currtabIndex: e.currentTarget.dataset.current,
        currtabState:false,
        storeList:[]
      })
      // //网络请求
      this.onRefresh();
    }else{
      this.setData({
        currtabState: !this.data.currtabState,
        isHiddenMenu: !this.data.isHiddenMenu
      })
    }
  },
  /**
   * pos部署
   * param：e  
   * saas四期：pos
   */
  chooseState:function(e){
    this.setData({
      currtabState: false,
      isHiddenMenu:true,
    })
    if (this.data.currtabIndex==1){
      //立马付
      this.setData({
        index: e.currentTarget.id
      })     
    }else {
      //pos
      this.setData({
        posState: e.currentTarget.id
      }) 
    }
    this.onRefresh();
  },
  /**
   * 新增部署方式，picker响应事件
   * param：e  
   * saas四期：批量入网
   */
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
    this.onRefresh();
  },

  _requestList: function () {
    let params = {
      pageNo: this.pageNo,
      pageSize: app.globalData.pageSize,
      // pageSize:'2'
    };
    if (this.data.currtabIndex==1){
      //立马付
      params.productId = "0"
      params.type = this.data.index.toString()
    }else{
      //pos机
      params.productId = "1"
      params.posState=this.data.posState
    }
    network_utils.buildRequest(constants.Interfaces.FUNC_MERCHANT_LIST, params, {
      doHttpSucess: (response) => {
        if (constants.RetCodes.SUCCESS === response.retCode) {
          this._requestSuccess(response);
          return true;
        }
        return false;
      },

      doComplete: function () {
        wx.stopPullDownRefresh();
        return false;
      }
    }).send();
  },

  onRefresh: function () {
    this.pageNo = "1";
    this._requestList();
  },

  /**
   * 下拉刷新事件
   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },

}, DeployingList));


